package com.xysq.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.CustomerBO;
import com.xysq.XYApplication;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.ToastUtil;
import com.xysq.util.UseUtil;
import com.xysq.util.UserInfoKeeper;

import butterknife.InjectView;

public class AuthorInformationActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
    @InjectView(R.id.layout_tips)
    LinearLayout tipsLayout;
    @InjectView(R.id.layout_skill)
    LinearLayout skillLayout;
    @InjectView(R.id.img_head)
    ImageView headImg;
    @InjectView(R.id.img_sex)
    ImageView sexImg;
    @InjectView(R.id.txt_name)
    TextView nameTxt;
    @InjectView(R.id.txt_school)
    TextView schoolTxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_author_information);
        initView();
        loadData();
    }

    private void loadData() {
        loadCustomer(UserInfoKeeper.readKcouponId(this));
    }

    private void initView() {
        tipsLayout.setOnClickListener(this);
        skillLayout.setOnClickListener(this);
        backIbtn.setOnClickListener(this);
    }

    private void loadCustomer(final String customerId) {
        appAction.getCustomer("", "", String.valueOf(customerId), new CallbackListener<CustomerBO>() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onSuccess(CustomerBO data) {
                Log.e("++++++", "获取用户信息成功" + " imgUrl" + " " + data.getHeadUrl());
                if (!TextUtils.isEmpty(data.getHeadUrl()) && !data.getHeadUrl().equals("") && !data.getHeadUrl().equals("null")) {
                    String imageUrl = data.getHeadUrl();
                    if (imageUrl.startsWith("http://")) {
                        XYApplication.getImageLoader().displayImage(imageUrl, headImg);
                    } else {
                        XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + imageUrl, headImg);
                    }
                }
//                Log.e("this is sex", data.getHeadUrl());
                if (null != data.getSex()) {
                    setSexImg(data.getSex());
                }
                String name = UseUtil.selectName(data.getNickname(), data.getName(), data.getPhone());
                nameTxt.setText(name);
                schoolTxt.setText(data.getSchoolName());
            }

            @Override
            public void onFailure(String errorCode, String message) {
                ToastUtil.showShort(AuthorInformationActivity.this, "获取用户信息失败" + message + customerId);
//                reloadingLayout.setVisibility(View.VISIBLE);
//                bottomLayout.setVisibility(View.GONE);
            }
        });
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void setSexImg(String sex) {
        if (sex.equals("1")) {
            sexImg.setBackground(getResources().getDrawable(R.drawable.ic_nan));
        } else {
            sexImg.setBackground(getResources().getDrawable(R.drawable.ic_nv));
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ibtn_back:
                finish();
                break;
            case R.id.layout_skill:
                Intent intentAuthorSkill = new Intent(this, AuthorSkillActivity.class);
                startActivity(intentAuthorSkill);
                break;
            case R.id.layout_tips:
                Intent intentTips = new Intent(this, AuthorTipsActivity.class);
                startActivity(intentTips);
                break;
        }
    }
}
